﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using BasicFramework.Devices.DirectX9;
using BasicFramework.Helper;
using BasicFramework.Rendering.Primitives;
using SlimDX;
using SlimDX.Direct3D9;
using BasicFramework.Rendering.Primitives.Indexes;
using System.Diagnostics;

namespace BasicFramework.Rendering.Buffer.DirectX9
{
    public class DX9DynamicIndexBuffer<T> : DynamicPrimitiveBuffer<T> where T : struct, IPrimitive
    {
        #region Fields

        private IndexBuffer iBuffer;
        private IndexBufferDescription description;
        private DX9DeviceContext context;

        #endregion


        #region Properties

        #endregion


        #region Constructors

        public DX9DynamicIndexBuffer(DX9DeviceContext context)
        {
            if (context == null)
            {
                Logger.Error("No DeviceContext specified !");
                Debug.Fail("No DeviceContext specified !");
            }
            this.context = context;

        }

        #endregion


        #region Events

        #endregion


        #region Methods

        internal override void FillBuffer()
        {
            DataStream stream = iBuffer.Lock(0, 0, LockFlags.Discard);
            stream.Seek(0, System.IO.SeekOrigin.Begin);
            try
            {
                Int16[] indexes = new Int16[this.Primitives.Count];
                for (int i = 0; i < Primitives.Count; i++)
                {
                    indexes[i] = ((Index)(this.Primitives[i] as IPrimitive)).Value;
                }
                // TODO : Buffer gerade ziehen

                stream.WriteRange(indexes);
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
            }
            finally
            {
                this.iBuffer.Unlock();
            }
        }

        internal override void SetPrimitiveDeclaration()
        {
            //description.Format == this.
        }

        internal override void SetBufferInternal(int stream)
        {
            this.context.Device.Indices = this.iBuffer;
        }

        internal override void ResizeBuffer(int newSize)
        {
            if (this.context == null)
            {
                Logger.Error("No Device Context specified !");
                Debug.Fail("No Device Context specified !");
                return;
            }
            if (iBuffer != null)
            {
                this.iBuffer.Dispose();
            }

            // Create a new Vertex Buffer
            this.iBuffer = new IndexBuffer(
                this.context.Device,
                newSize,
                Usage.Dynamic | Usage.WriteOnly,
                Pool.Default,
                true
            );

        }

        internal override void DisposeBuffer()
        {
            if (this.iBuffer != null)
            {
                this.iBuffer.Dispose();
            }
        }

        #endregion




    }
}
